package adapter

import (
	"github.com/stretchr/testify/assert"
	"migration-tool/common"
	"testing"
)

func TestCreateOneAdapter(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	cfg.BaseConfig.Export = true
	cfg.BaseConfig.UseProgress = true
	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	args := DefaultAdapterArgs{
		RepoInfo: &repoInfo,
	}

	_, err = CreateOneAdapter(&cfg, args)
	assert.Equal(t, err, nil)
}

func TestGetAllRepoInfo(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	cfg.BaseConfig.Export = true
	cfg.BaseConfig.UseProgress = true

	exportRepos, err := GetAllRepoInfo(&cfg)
	assert.Equal(t, err, nil)

	importRepos, err := GetAllRepoInfo(&cfg)
	assert.Equal(t, err, nil)

	equal := true
	for _, exportVal := range exportRepos {
		e := false
		for _, importVal := range importRepos {
			if exportVal.RepoId == importVal.RepoId {
				e = true
			}
		}
		equal = e && equal
	}

	assert.Equal(t, equal, true)
}

func TestFlushAllUsersFromAllRepo(t *testing.T) {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	cfg.BaseConfig.Export = true
	cfg.BaseConfig.UseProgress = true

	repos := make(map[int64]*RepoInfo)
	repoInfo1 := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}

	repoInfo2 := RepoInfo{
		RepoId:           1549172,
		RepoGroupName:    "agit",
		RepoName:         "satellite",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/satellite.git",
		WikiEnable:       true,
	}

	repos[repoInfo1.RepoId] = &repoInfo1
	repos[repoInfo2.RepoId] = &repoInfo2

	assert.Equal(t, err, nil)

}

func TestExportUseProgressStartGlobalTask(t *testing.T) {
	cfg := getUseProgressConfig(t)
	cfg.BaseConfig.Export = true

	err := StartGlobalTask(cfg)
	assert.Equal(t, err, nil)
}

func TestExportNotUseProgressStartGlobalTask(t *testing.T) {
	cfg := getNotUseProgressConfig(t)
	cfg.BaseConfig.Export = true

	err := StartGlobalTask(cfg)
	assert.Equal(t, err, nil)
}

func TestImportUseProgressStartGlobalTask(t *testing.T) {
	cfg := getUseProgressConfig(t)
	cfg.BaseConfig.Export = false

	err := StartGlobalTask(cfg)
	assert.Equal(t, err, nil)
}

func TestImportNotUseProgressStartGlobalTask(t *testing.T) {
	cfg := getNotUseProgressConfig(t)
	cfg.BaseConfig.Export = false

	err := StartGlobalTask(cfg)
	assert.Equal(t, err, nil)
}
